/**
 * 
 */
package com.base.myproject.client.ui;

import com.base.myproject.client.tools.fileupload.BaseProgressX;
import com.base.myproject.client.tools.fileupload.IUploader;
import com.base.myproject.client.tools.fileupload.MultiUploader;
import com.base.myproject.client.tools.fileupload.PreloadedImage;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.FlowPanel;

/**
 * @author zgw 2009-8-7 下午08:47:44 javazgw@gmail.com
 */
public class BaseFileUploadDialog {

	static BaseFileUploadDialog basefileuploaddialog;
	 Dialog dialog;
	final MultiUploader uploader = new MultiUploader(new BaseProgressX());

	String tablename,columns,values;
	public BaseFileUploadDialog() {
		init();
	}
	/**
	 * columns和values 多个字段用，逗号分开，表内默认要有imagepath这个字段。
	 * BaseFileUploadDialog bud = new BaseFileUploadDialog("b2cgcodeimage","upicode,id,","'123','321',");
				bud.show();
	 * @param tablename
	 * @param columns
	 * @param values
	 */
	public BaseFileUploadDialog(String tablename,String columns,String values)
	{
		this.tablename = tablename;
		this.columns = columns;
		this.values = values;
		uploader
		.setServletPath("gupld?tableinftablename="+tablename+"&columns="+columns+"&values="+values+"&imagepath=imagepath");
		init();
	}
	/**
	 * columns和values 多个字段用，逗号分开，表内默认要有imagepath这个字段。
	 * BaseFileUploadDialog bud = new BaseFileUploadDialog("b2cgcodeimage","upicode,id,","'123','321',","imagepath","bcodemage");
	 * bud.show();
	 * 指定图片存储字段imagepath，文件夹filestorepath，表名b2cgcodeimage，默认值"upicode,id,","'123','321'," 注意格式
	 * @param tablename
	 * @param columns
	 * @param values
	 * @param imagepath
	 * @param filestorepath
	 */
	public BaseFileUploadDialog(String tablename,String columns,String values,String imagepath,String filestorepath)
	{
		this.tablename = tablename;
		this.columns = columns;
		this.values = values;
		uploader
		.setServletPath("gupld?tableinftablename="+tablename+"&columns="+columns+"&values="+values+"&imagepath="+imagepath+"&filestorepath="+filestorepath);
		init();
	}

	void init() {
		dialog = new Dialog();
		dialog.setBodyBorder(false);
		dialog.setIconStyle("balloons");
		dialog.setHeading("请选择文件上传");
		dialog.setWidth(400);
		dialog.setHeight(225);
		//dialog.setHideOnButtonClick(true);
		BorderLayout layout = new BorderLayout();
		dialog.setLayout(layout);
		HorizontalPanel panel = new HorizontalPanel();

//		uploader
//				.setServletPath("servlet.gupld?tableinftablename=b2cgcodeimage&imagepath=imagepath");
		// MultiUploader uploader = new MultiUploader(new BasicProgress());
		panel.add(uploader);
		final FlowPanel panelImages = new FlowPanel();

		final ValueChangeHandler<PreloadedImage> showImage = new ValueChangeHandler<PreloadedImage>() {
			// Attach an image to the pictures viewer
			public void onValueChange(ValueChangeEvent<PreloadedImage> event) {
				PreloadedImage img = event.getValue();
				;
				img.setWidth("75px");

				panelImages.add(img);
			}
		};
		ValueChangeHandler<IUploader> onFinishHandler = new ValueChangeHandler<IUploader>() {
			// preload the image in the document and in the case of success
			// attach it to the viewer
			public void onValueChange(ValueChangeEvent<IUploader> event) {
				// 屏蔽结束后的操作
				IUploader uploader = event.getValue();
				// System.out.println("showImage==="+showImage);
				// System.out.println("uploader.fileUrl()"+uploader.fileUrl());
				new PreloadedImage(uploader.fileUrl(), showImage);
			}
		};

		// Add a finish handler to the uploader
		uploader.setOnFinishHandler(onFinishHandler);
		uploader.setOnStartHandler(new ValueChangeHandler() {

			@Override
			public void onValueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				// System.out.println("onValueChange.............start!");
				// uploader.setServletPath("servlet.gupld?zgw=zgwwwww&zgw3=ddddd");
			}

		});
		// Attach the image viewer to the document
		panelImages.setWidth("50%");
		panel.add(panelImages);
		panel.setWidth(500);
		panel.setScrollMode(Scroll.AUTO);
		BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
		dialog.add(panel, data);
		dialog.getButtonById(Dialog.OK).addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				post();
				dialog.hide();
				
				dialog=null;
			}
			
		});
	}

	@Deprecated
	public static BaseFileUploadDialog getInstance() {
		if (basefileuploaddialog == null) {
			basefileuploaddialog = new BaseFileUploadDialog();
		}
		return basefileuploaddialog;
	}

	public void show() {
		dialog.show();
	}

	public void setServletPath(String path) {
		// servletPath = path;
		uploader.setServletPath(path);
	}
	/**
	 * 子类继承用
	 */
	public void post()
	{
		
	}
	

}
